package com.example.webbasic.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.function.Consumer;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 第二个Servlet
 * @author ben
 * @date 2021-11-06 15:31:15 CST
 */
@WebServlet(name = "myServlet2", urlPatterns = {"/a/*", "/b/*"}, description = "这是MyServlet2")
public class MyServlet2 extends HttpServlet {

	private static final long serialVersionUID = 211106L;
	
	private static Consumer<Object> cs = System.out::println;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
    {
        cs.accept("MyServlet2 doGet: uri=" + req.getRequestURI() + ", rp=" + req.getRemotePort() + ", lp=" + req.getLocalPort()
        		+ ", sp=" + req.getServerPort());
        
        cs.accept("MyServlet2 doGet: url=" + req.getRequestURL() + ", laddr=" + req.getLocalAddr() + ", raddr=" + req.getRemoteAddr());
        cs.accept("MyServlet2 sessionId=" + req.getRequestedSessionId());
        this.doPost(req, resp);
    }

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
        cs.accept("MyServlet2 doPost: uri=" + req.getRequestURI());
        cs.accept("MyServlet2 doPost: url=" + req.getRequestURL());

        // 响应
        resp.setContentType("text/plain");
        resp.setCharacterEncoding("utf-8");
        
        PrintWriter out = resp.getWriter();
        out.print("MyServlet2 url: " + req.getRequestURL());
        out.print("\r\nMyServlet2 服务器时间：" + new Date());
    }
	
}
